package com.shchuangzhi.smartguard.core;

import android.content.SharedPreferences;

import com.elvishew.xlog.XLog;
import com.shchuangzhi.smartguard.R;

/**
 * Created by j_hu on 7/17/17.
 */

public class Mode {
    private boolean tick = true;
    private boolean tack = true;
    private int current = Consts.IN_MODE;
    private SharedPreferences preferences;
    private static final String MODE_PREFERENCE = "mode_preference";
    private static final String AUTO_SWITCH_PREFERENCE = "auto_switch_preference";
    private static final String MODE_SWITCH_TIME_PREFERENCE = "mode_switch_time_preference";
    private static final Mode instance = new Mode();

    public void in() {
        current = Consts.STATE_HEALTH;

        if (preferences != null) {
            SharedPreferences.Editor editor = preferences.edit();
            editor.putBoolean(MODE_PREFERENCE, true);
            editor.commit();
        }
    }

    public void out() {
        current = Consts.STATE_OUT;

        if (preferences != null) {
            SharedPreferences.Editor editor = preferences.edit();
            editor.putBoolean(MODE_PREFERENCE, false);
            editor.commit();
        }
    }

    public void reset() {
        tick = true;
        tack = true;
        in();
    }

    public int getCurrent() {
        return current;
    }

    public void init(SharedPreferences preferences) {
        this.preferences = preferences;

        Job modeSwitchJob = JobManager.get(Consts.JOB_AUTO_MODE_SWITCH);
        if (modeSwitchJob != null) {
            Job.JobDataHolder holder = (Job.JobDataHolder)modeSwitchJob.getData();
            holder.addValue(preferences);
        }

        in();
    }

    public void startAuto() {
        final Job.JobDataHolder holder = new Job.JobDataHolder();
        JobManager.add(new Job(Consts.JOB_AUTO_MODE_SWITCH,
                Consts.INTERVAL_AUTO_MODE_SWITCH,
                new Job.JobExecutor(){
                    @Override
                    public Object Execute() {
                        autoSwitch(holder);
                        return null;
                    }
                }, holder));
    }

    public void saveChangedConfig(boolean autoSwitchEnabled, String time) {
        if (preferences != null) {
            if (autoSwitchEnabled) {
                if (time != null && time.matches("^\\d{2}:\\d{2}$")) {
                    SharedPreferences.Editor editor = preferences.edit();
                    editor.putString(MODE_SWITCH_TIME_PREFERENCE, time);
                    editor.commit();
                }
            }

            SharedPreferences.Editor editor = preferences.edit();
            editor.putBoolean(AUTO_SWITCH_PREFERENCE, autoSwitchEnabled);
            editor.commit();
        }

    }

    public static Mode getInstance() {
        return instance;
    }

    private boolean switchIn() {
        boolean switched = false;
        if (tick) {
            tick = false;
            tack = true;

            in();
            switched = true;
        }
        return switched;
    }

    private boolean switchOut() {
        boolean switched = false;
        if (tack) {
            tack = false;
            tick = true;

            out();
            switched = true;
        }
        return switched;
    }

    private void autoSwitch(Job.JobDataHolder holder) {
        SharedPreferences sp = (SharedPreferences)holder.getFirst();
        if (sp != null) {
            boolean autoModeSwitch = sp.getBoolean(AUTO_SWITCH_PREFERENCE, false);
            if (autoModeSwitch) {
                String switchTime = sp.getString(MODE_SWITCH_TIME_PREFERENCE, "");
                boolean tt = Clock.nowStr("HH:mm").compareTo(switchTime) >= 0 ? switchOut() : switchIn();
                if (tt) {
                    NotifyPrinter.getInstance().notify(
                            NotifyMessage.wrap("",
                                    Consts.NOTIFY_STATE_BUTTON_SWITCH_MODE,
                                    Consts.NOTIFY_INOUT_LIST_SWITCH_MODE,
                                    Consts.NOTIFY_MAIN_OP_SWITCH_MODE,
                                    Consts.NOTIFY_SCHOOL_CHANGED));
                }
            }
        }
    }
}
